<?php
/**
 * wondershare站的下载页面接口文件
 * @auth huangcb
 * */
//ini_set('display_errors','On');
//error_reporting( E_ALL );

define('INTERFACE_CODE', md5('ws-product-downloads'));
define('DB_TABLE_PRE','');//表前缀
define('CACHE_DIR', '../../image2/cache/');//缓存文件夹

//note 接口合法性验证
if( $_GET['code'] != INTERFACE_CODE ) exit('error code!');
require_once '../../conf/conf.php';
require_once '../../translate/class/Mysql.class.php';
require_once '../../inc/Function.php';
$db = new Mysql();
$GLOBALS['db']= $db;


function get_all_products( $db ){
	
	$os = $_GET['type'];
	if( $os && $os == 'windows' ) $os = 'win';
	
	//note 有缓存的话直接输出缓存
	$tmp = CACHE_DIR.'ws_download-all_products_'.($os ? $os : 'all').'_tmp.php';
	if( file_exists( $tmp ) ){
		echo file_get_contents( $tmp );
		exit;
	}

	$where = ($os ? ($os == 'win'? "p.product_os = 'Win'" : "p.product_os = 'Mac'") : "(p.product_os = 'Win' OR p.product_os = 'Mac')");

	//取得ws中的产品与单页管理中的产品
	$sql = "SELECT 
			p.product_id, p.product_name, p.product_os, p.product_isfree, p.product_type, p.product_support_intro, p.product_intro, p.product_cbsid,
			v.v_id,v.version_name,v.download_url,v.product_size,
			wp.won_product_id, wp.is_emphasis, wp.cat_id, wp.parent_path,
			t.url
			FROM
			
			(SELECT pid, url FROM (
				SELECT t26.Field_20044 AS pid, t26.url FROM Table_26 AS t26 WHERE t26.is_open = 0
				UNION
				SELECT t45.Field_20350 AS pid, t45.url AS pid FROM Table_45 AS t45 WHERE t45.is_open = 0 AND t45.Field_20361 = 1
				) AS tf
			)AS t
			
			LEFT JOIN (
				SELECT w.id, w.won_product_id, w.is_emphasis, c.cat_id, c.parent_path FROM won_product AS w 
				LEFT JOIN category AS c ON w.cat_id = c.cat_id
			) as wp ON( t.pid = wp.id )
			
			LEFT JOIN product AS p ON p.product_id = wp.won_product_id
			LEFT JOIN version AS v ON (t.pid = v.product_id AND v.is_sale = 1)
			
			WHERE {$where} AND wp.cat_id > 0 AND p.product_type = 1 AND p.product_delete = 0 AND p.product_brand = 'Wondershare' AND v.version_name != ''
			ORDER BY wp.is_emphasis DESC, p.product_id DESC
	";

	$p = $db->getAll( $sql );
	
	//要显示的产品
	$arr = array(31,191,6,678,595,751,526,619,465,49,1010,386,235,905,1052,1037,974,944,1084,902,899,1066,998,911,914,916,920,923,926,935,938);
	foreach( $p as $k=>$v ){
		if( !in_array( $v['product_cbsid'], $arr ) ){
			unset( $p[$k] );
			continue;
		}
	}

	//将原始数据写缓存，在get_product_name方法中用到
	file_put_contents( CACHE_DIR.'ws_download-all_products_'.($os ? $os : 'all').'_ori_tmp.php', serialize($p) );
	
	//对数据进行过滤
	
	foreach( $p as $k=>$v ){
		$a = str_replace('/wondershare.fr/httpdocs/','http://www.wondershare.fr/', $v['url']);
		$a = substr( $a, -10) == 'index.html' ? substr( $a, 0, -10) : $a;
		$p[$k]['url'] = $a;
	}
	
	$arr_1 = array();
	foreach( $p as $k=>$v ){
		$arr_1[ str_replace(' ', '', strtolower( $v['product_name'] ) ) ] = $k;
	}
	
	//将产品中xxx和xxx for Mac 合并成一项
	$arr_2 = array();
	foreach( $p as $k=>$v ){
		if( isset( $arr_1[ str_replace(' ', '', strtolower( $v['product_name'].'formac' ) ) ] ) ){
			$arr_2[ $v['product_name'] ]['Win'] = $p[$k];
		}else{
			$n = str_replace(' ', '', strtolower(substr( $v['product_name'], 0, strlen($v['product_name'])-8 )));
			if( isset($arr_1[$n]) ){
				$arr_2[ substr( $v['product_name'], 0, strlen($v['product_name'])-8 ) ][$v['product_os']] = $p[$k];
			}else{
				$arr_2[ $v['product_name'] ][$v['product_os']] = $p[$k];
			}	
		}
	}
	
	//print_r( $arr_2 );exit;

	//取得根分类
	$sql = "SELECT cat_id,cat_title FROM category WHERE cat_type = 2 AND cat_web = 1 AND is_delete = 0 AND cat_series = 1 ORDER BY cat_id ASC";
	$c = $db->getAll( $sql );
	$cat = array();
	foreach( $c as $v ){
		$cat[$v['cat_id']] = $v['cat_title'];
	}
	
	//将产品以根类别分类
	$arr_3 = array();
	foreach( $arr_2 as $k=>$v ){
		$cat_arr = explode(',', isset($v['Win']['parent_path']) ? $v['Win']['parent_path'] : $v['Mac']['parent_path']);
		$arr_3[$cat[$cat_arr[0]]][$k] = $arr_2[$k];
	}
	
	
	//print_r( $arr_3 );exit;
	$ret = serialize( $arr_3 );
	
	//写缓存
	file_put_contents( $tmp, $ret );	
	echo $ret;
}

//根据条件获取产品的最新版本，以及产品名字，链接 ，下载地址
function get_products_info($db)
{
	$where_str = ' WHERE 1=1 ';
	$where_str .= empty($_GET['is_free'])?'':' AND p.product_isfree = "'.trim(htmlspecialchars($_GET['is_free'])).'"';//是否免费
	$where_str .= empty($_GET['os_type'])?' AND p.product_os IN ("Win","Mac")':' AND p.product_os = "'.trim(htmlspecialchars($_GET['os_type'])).'"';//存储平台表
	$where_str .= empty($_GET['web_type'])?'': ' AND p.product_brand = "'.trim(htmlspecialchars($_GET['web_type'])).'"';//产品类型
	$sql = 'SELECT won_p.id,p.product_id,p.product_name, tab.url,v.version_name,v.download_url FROM won_product won_p  
			LEFT JOIN product p ON p.product_id = won_p.won_product_id
			LEFT JOIN version v ON v.product_id  = p.product_id 
			LEFT JOIN Table_26 tab ON tab.Field_20044 = won_p.id  
			'.$where_str.' AND v.is_sale = 1 AND won_p.won_product_delete = 0 AND !FIND_IN_SET(won_p.id ,"116") ORDER BY won_p.is_emphasis DESC LIMIT 5';
	$data = $db->getAll( $sql );
	foreach( $data as $k => $v ){
		if(empty($v['url']))
		{
			$sql = 'SELECT url FROM Table_45 WHERE Field_20350 = '.$v['id'].' AND Field_20361 = 1';
			$rows = $db->getOne( $sql );
			$v['url'] = $rows['url'];
		}
		
		$data[$k]['url']  = str_replace(array('/wondershare.fr/httpdocs/','/index.html'),array('http://www.wondershare.fr/',''), $v['url']);
	}

	unset($version);
	echo serialize( $data );
	
}

//note 取得最近推出
function get_new_release( $db ){
	
	$os = $_GET['type'];
	if( $os && $os == 'windows' ) $os = 'win';
	
	$tmp = CACHE_DIR.'ws_download-new_release_'.($os ? $os : 'all').'_tmp.php';

	//读缓存
	if( file_exists( $tmp ) ){
		echo file_get_contents( $tmp );
		exit;
	}


	$where = ($os ? ($os == 'win'? "p.product_os = 'Win'" : "p.product_os = 'Mac'") : "(p.product_os = 'Win' OR p.product_os = 'Mac')");

	$sql = "SELECT p.product_id, p.product_name, p.product_isfree, p.product_type, p.product_cbsid,
			v.version_name,v.download_url,t.url			
			FROM 
			(SELECT pid, url FROM (
				SELECT t26.Field_20044 AS pid, t26.url FROM Table_26 AS t26 WHERE t26.is_open = 0
				UNION
				SELECT t45.Field_20350 AS pid, t45.url AS pid FROM Table_45 AS t45 WHERE t45.is_open = 0 AND t45.Field_20361 = 1
				) AS tf
			)AS t
			LEFT JOIN won_product AS wp ON t.pid = wp.id
			LEFT JOIN product AS p ON wp.id = p.product_id
			LEFT JOIN version AS v ON (t.pid = v.product_id AND v.is_sale = 1)	
			WHERE {$where} AND p.product_type = 1 AND p.product_brand = 'Wondershare' AND wp.won_product_delete = 0 AND v.version_name != ''
			ORDER BY p.product_id DESC LIMIT 5
	";
	
	$data = $db->getAll( $sql );
	
	//要显示的产品
	$arr = array(447,579,891,448,575,1045,1070,1080,892,850,963,757,758,593,826,529,820,544,886,881,882,545,527,537,729,1020,1019,1018,383,31,191,6,678,595,751,526,619,465,49,737,1010,386,235);

	//对数据进行过滤
	foreach( $data as $k=>$v ){
		
		if( !in_array( $v['product_cbsid'], $arr ) ){
			unset( $data[$k] );
			continue;
		}
		
		$a = str_replace('/wondershare.fr/httpdocs/','http://www.wondershare.fr/', $v['url']);
		$a = substr( $a, -10) == 'index.html' ? substr( $a, 0, -10) : $a;
		$data[$k]['url'] = $a;
	}
	
	//print_r( $data );exit;
	$ret = serialize( $data );
	
	//写缓存
	file_put_contents( $tmp, $ret );
	
	echo $ret;
}

//note 取得所有产品的名称
function get_product_name( $db ){
	
	$os = $_GET['type'];
	if( $os && $os == 'windows' ) $os = 'win';
	
	//直接读缓存文件
	$tmp = CACHE_DIR.'ws_download-all_products_'.($os ? $os : 'all').'_ori_tmp.php';	
	$ret = file_get_contents( $tmp );
	$arr = unserialize( $ret );
	
	$p = array();
	foreach( $arr as $v ){
		$p[] = $v['product_name'];
	}
	
	//print_r( $p );exit;
	echo serialize( $p );
}

//note 取得根分类
function get_root_cat( $db ){
	
	$tmp = CACHE_DIR.'ws_download-root_cat_tmp.php';
	//读缓存
	if( file_exists( $tmp ) ){
		echo file_get_contents( $tmp );
		exit;
	}
	
	$sql = "SELECT cat_title FROM category WHERE cat_type = 2 AND cat_web = 1 AND is_delete = 0 AND cat_series = 1 ORDER BY cat_id ASC";
	$cat = $db->getAll( $sql );
	
	$arr = array();
	foreach( $cat as $v ){
		$arr[] = $v['cat_title'];
	}
	
	$ret = serialize( $arr );
	
	//写缓存
	file_put_contents( $tmp, $ret );
	
	echo $ret;
}

//note 设置产品大小
function set_product_size( $db ){
	
	$tmp = CACHE_DIR.'ws_download-product_size_tmp.php';
	
	$time = $num = 0;
	//读缓存
	if( file_exists( $tmp ) ){
		$c = file_get_contents( $tmp );
		$a = explode('-', $c);
		$time = (int) $a[0];
		$num = (int) $a[1];
		
		//如果一分钟内请求超过5次，则不执行取文件大小。取文件大小非常耗时
		if( (time() - $time) < 60 && $num > 5 ){
			echo serialize( array('size'=>0) );
			exit;
		}
		
	}
	
	$v_id = $_GET['v_id'];
	$url = urldecode( $_GET['url'] );
	
	$f = get_headers( $url, 1 );
	
	$size = 0;
	foreach( $f['Content-Length'] as $v){
		$size = max($size, $v );
	}
	$size = sprintf('%0.2f', $size / 1000000);
	
	$db->update('version',array('product_size'=>$size),'v_id='.$v_id);
	
	if( (time() - $time) < 60 ){
		$num++;
	}else{
		$time = time();
		$num = 0;
	}
	
	file_put_contents( $tmp, $time.'-'.$num);
	
	echo serialize( array('size'=>$size) );
}

//note 搜索产品
function search_product(){
	$p = strtolower( $_GET['name'] );
	$os = $_GET['type'];
	if( $os && $os == 'windows' ) $os = 'win';
	
	//搜索的话则从缓存中读出搜索的产品
	$tmp = CACHE_DIR.'ws_download-all_products_'.($os ? $os : 'all').'_tmp.php';
	$ret = file_get_contents( $tmp );

	$arr = unserialize( $ret );//print_r( $arr );exit;
	
	foreach( $arr as $k=>$v ){
		foreach( $v as $kk=>$vv ){
			if( isset( $vv['Win']) ){
				if( strtolower( $vv['Win']['product_name'] ) != $p ){
					unset( $arr[$k][$kk]['Win'] );
				}
			}

			if( isset( $vv['Mac']) ){
				if( strtolower( $vv['Mac']['product_name'] ) != $p  ){
					unset( $arr[$k][$kk]['Mac'] );
				}
			}
			if( empty( $arr[ $k ][$kk] ) ) unset( $arr[ $k ][$kk] );
		}
		if( empty( $arr[ $k ] ) ) unset( $arr[ $k ] );
	}
		
			
	$a = array_values( $arr );
	$a = array_values( $a[0] );
	$a = array_values( $a[0] );
	if( !empty( $a[0]) ) $a[0]['url'] = str_replace('/wondershare.fr/httpdocs/','http://www.wondershare.fr/',$a[0]['url']);
	//print_r( $a[0] );exit;
	echo serialize( $a[0] );
}


$d = $_GET['d'];
$d( $db );
